<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Category\StoreCategoryRequest;
use App\Http\Requests\Tag\TagCategoryRequest;
use App\Models\Tag;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;

class TagController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(): View
    {
        $tags = Tag::paginate(5);
        return view('admin.tags.index', ['tags' => $tags]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create(): View
    {
        return view('admin.tags.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(TagCategoryRequest $request): \Illuminate\Http\RedirectResponse
    {
        Tag::create($request->all());
//        $request->session()->flash('$messages',  'Tag created successfully');
        return redirect()->route('tags.index')->with('message', 'Tag created successfully');
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id): View
    {
        $category = Tag::find($id); //находим категорию
        return view('admin.tags.edit', ['tags' => $category]); //возвращаем форму редактирования
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(StoreCategoryRequest $request, string $id)
    {
        $category = Tag::find($id);
        $category->slug = null;
        $category->update($request->all());
        $request->session()->flash('$messages', 'Tag updated successfully');
        return redirect()->route('tags.index')->with('message', 'Tag updated successfully');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        Tag::destroy($id);
        return redirect()->route('tags.index', ['message' => 'Tag deleted successfully']);
    }
}
